package com.example.smart_city_0829.mainFragment;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.smart_city_0829.R;
import com.example.smart_city_0829.activity.lawyer_consultation.LawyerMainActivity;
import com.example.smart_city_0829.activity.logistics_inquiries.LogisticsInquiriesMainActivity;
import com.example.smart_city_0829.activity.pet_hos.PetHospitalActivity;
import com.example.smart_city_0829.activity.volunteering.VolunteeringMainActivity;
import com.example.smart_city_0829.activity.waste_sorting.WasteSortingMainActivity;
import com.example.smart_city_0829.activity.youth_lnn.YouthInnActivity;
import com.example.smart_city_0829.activity.commonweal.CommonwealMainActivity;
import com.example.smart_city_0829.activity.digitallibrary.DigitalLibraryActivity;
import com.example.smart_city_0829.activity.government.GovernmentMainActivity;
import com.example.smart_city_0829.base.BaseFragment;
import com.example.smart_city_0829.base.CommonAdapter;
import com.example.smart_city_0829.entity.server.ServerList;
import com.example.smart_city_0829.retrofit.NetCallBack;
import com.example.smart_city_0829.utils.Constant;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import retrofit2.Call;
import retrofit2.Response;

public class AllServeFragment extends BaseFragment {

    private ListView serverTypeTab;
    private GridView serverGrid;

    //所有服务数据
    private List<ServerList.RowsBean> servers;
    //所有服务分类
    private List<String> sortedServerStringType;
    private ServerGvAdapter serverGvAdapter;
    //当前展示的服务列表
    private List<ServerList.RowsBean> showingServers;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_all_server;
    }

    @Override
    protected void initView() {
        serverTypeTab = (ListView) rootView.findViewById(R.id.server_type_tab);
        serverGrid = (GridView) rootView.findViewById(R.id.server_grid);
    }

    @Override
    protected void initData() {
        getAllServer();
    }

    @Override
    protected void initEvent() {
//        migrateTo(GovernmentMainActivity.class);

        serverTypeTab.setOnItemClickListener((parent, view, position, id) -> {
            Log.e("chooseServerType", sortedServerStringType.get(position));
            showServers(sortedServerStringType.get(position));
        });

        serverGrid.setOnItemClickListener((parent, view, position, id) -> {
            final ServerList.RowsBean rowsBean = showingServers.get(position);
            switch (rowsBean.getServiceName()) {
                case "数字图书馆":
                    migrateTo(DigitalLibraryActivity.class);
                    break;
                case "青年驿站":
                    migrateTo(YouthInnActivity.class);
                    break;
                case "爱心捐赠":
                    migrateTo(CommonwealMainActivity.class);
                    break;
                case "政府服务热线":
                    migrateTo(GovernmentMainActivity.class);
                    break;
                case "垃圾分类":
                    migrateTo(WasteSortingMainActivity.class);
                    break;
                case "宠物医院":
                    migrateTo(PetHospitalActivity.class);
                    break;
                case "法律咨询":
                    migrateTo(LawyerMainActivity.class);
                    break;
                case "志愿服务":
                    migrateTo(VolunteeringMainActivity.class);
                    break;
                case "物流查询":
                    migrateTo(LogisticsInquiriesMainActivity.class);
                    break;
                default:
                    break;
            }
        });
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            if (sortedServerStringType != null && !sortedServerStringType.isEmpty() && servers != null && !servers.isEmpty()) {
                showServers(sortedServerStringType.get(0));
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        getAllServer();
    }

    /**
     * 获取所有服务
     */
    private void getAllServer() {
        api.getServerList().enqueue(new NetCallBack<ServerList>() {
            @Override
            public void onResponse(Call<ServerList> call, Response<ServerList> response) {
                try {
                    //拿到所有服务
                    servers = response.body().getRows();
                    //为服务分类
//                    final Map<Integer, List<ServerList.RowsBean>> sortServer = servers.stream().collect(Collectors.groupingBy(ServerList.RowsBean::getSort));
                    final Map<String, List<ServerList.RowsBean>> sortServer = servers.stream().collect(Collectors.groupingBy(ServerList.RowsBean::getServiceType));

//                    sortServer.forEach(
//                            (sortId,data) -> {
//
//                            }
//                    );
//                    final List<String> sortId = sortServer.keySet().stream().map(String::valueOf).collect(Collectors.toList());
                    //所有服务总类
                    sortedServerStringType = new ArrayList<>(sortServer.keySet());
                    //组装左边分类数据
                    serverTypeTab.setAdapter(new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_dropdown_item, sortedServerStringType));
                    //显示第一种类
                    showServers(sortedServerStringType.get(0));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void showServers(String type) {
        //查询对应服务
        showingServers = servers.stream().filter((data) -> data.getServiceType().equals(type)).collect(Collectors.toList());
        //显示数据
        serverGvAdapter = new ServerGvAdapter(showingServers, requireContext());
        serverGrid.setAdapter(serverGvAdapter);

    }

    private static class ServerGvAdapter extends CommonAdapter<ServerList.RowsBean, ServerViewHolder> {

        public ServerGvAdapter(List<ServerList.RowsBean> data, Context context) {
            super(data, context);
        }

        @Override
        protected void setData(ServerList.RowsBean data, int position, ServerViewHolder viewHolder, View convertView, Context context) {
            Glide.with(context)
                    .load(Constant.BASE_URL2 + data.getImgUrl())
                    .into(viewHolder.im);

            viewHolder.serverName.setText(String.valueOf(data.getServiceName()));
        }


        @Override
        protected ServerViewHolder getViewHolder() {
            return new ServerViewHolder();
        }

        @Override
        protected void initView(ServerViewHolder viewHolder, View convertView) {
            viewHolder.im = (ImageView) convertView.findViewById(R.id.im);
            viewHolder.serverName = (TextView) convertView.findViewById(R.id.server_name);
        }

        @Override
        protected int getItemLayoutResourceId() {
            return R.layout.server_list_item;
        }
    }

    private static class ServerViewHolder {
        ImageView im;
        TextView serverName;
    }
}
